package cn.bocweb.spinning.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.StartExamBean;
import cn.bocweb.spinning.view.activity.TestActivity;

/**
 * Created by chenchangxing on 15/12/23.
 */
public class ExamQuizFragment extends Fragment implements View.OnClickListener {


    int resId, i;
    @Bind(R.id.exam_quiz_type)
    TextView examQuizType;
    @Bind(R.id.exam_quiz_quiz)
    TextView examQuizQuiz;
    @Bind(R.id.exam_quiz_judge_a_icon)
    ImageView examQuizJudgeAIcon;
    @Bind(R.id.exam_quiz_judge_a)
    LinearLayout examQuizJudgeA;
    @Bind(R.id.exam_quiz_judge_b_icon)
    ImageView examQuizJudgeBIcon;
    @Bind(R.id.exam_quiz_judge_b)
    LinearLayout examQuizJudgeB;
    @Bind(R.id.exam_quiz_judge)
    LinearLayout examQuizJudge;
    @Bind(R.id.exam_quiz_select_a_icon)
    ImageView examQuizSelectAIcon;
    @Bind(R.id.exam_quiz_select_a)
    LinearLayout examQuizSelectA;
    @Bind(R.id.exam_quiz_select_b_icon)
    ImageView examQuizSelectBIcon;
    @Bind(R.id.exam_quiz_select_b)
    LinearLayout examQuizSelectB;
    @Bind(R.id.exam_quiz_select_c_icon)
    ImageView examQuizSelectCIcon;
    @Bind(R.id.exam_quiz_select_c)
    LinearLayout examQuizSelectC;
    @Bind(R.id.exam_quiz_select_d_icon)
    ImageView examQuizSelectDIcon;
    @Bind(R.id.exam_quiz_select_d)
    LinearLayout examQuizSelectD;
    @Bind(R.id.exam_quiz_select)
    LinearLayout examQuizSelect;

    StartExamBean.DataEntity dataEntities;
    TestActivity testActivity;
    Timer timer;
    Map<Integer, Boolean> answerList;
    int position;
    int judgeAnswer = 0;
    int selectAnswer = 0;
    boolean isSecondTest;
    @Bind(R.id.test_select_a)
    TextView testSelectA;
    @Bind(R.id.test_select_b)
    TextView testSelectB;
    @Bind(R.id.test_select_c)
    TextView testSelectC;
    @Bind(R.id.test_select_d)
    TextView testSelectD;
    @Bind(R.id.test_detail)
    TextView testDetail;
    @Bind(R.id.test_explain_layout)
    LinearLayout testExplainLayout;
    @Bind(R.id.test_layout)
    LinearLayout testLayout;


    public ExamQuizFragment(StartExamBean.DataEntity dataEntities, int position, boolean isSecondTest) {
        this.dataEntities = dataEntities;
        this.position = position;
        this.isSecondTest = isSecondTest;
    }

    public void setLayoutVisible(boolean isVisible) {
        testLayout.setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_exam_quiz, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        testActivity = (TestActivity) getActivity();
        timer = new Timer();
        answerList = new HashMap<>();

        initTitle();
        initClickBtn();
        initExplain();
    }

    @Override
    public void onStart() {
        super.onStart();

//        testActivity = (TestActivity) getActivity();
//        timer = new Timer();
//        answerList = new HashMap<>();
//
//        initTitle();
//        initClickBtn();
//        initExplain();
    }


    private void initExplain() {
        testDetail.setText(dataEntities.getExplain());
    }


    private void initTitle() {
        int type = Integer.parseInt(dataEntities.getTtype());
        switch (type) {
            case 1: {
                examQuizQuiz.setText(dataEntities.getTitle());
                examQuizSelect.setVisibility(View.VISIBLE);
                examQuizJudge.setVisibility(View.GONE);
                examQuizType.setText("选");
                initSelectQuiz();
                selectAnswer = Integer.parseInt(dataEntities.getRight());
                testDetail.setText(dataEntities.getExplain());
                break;
            }
            case 2: {
                examQuizQuiz.setText(dataEntities.getTitle());
                examQuizSelect.setVisibility(View.GONE);
                examQuizJudge.setVisibility(View.VISIBLE);
                examQuizType.setText("判");
                judgeAnswer = Integer.parseInt(dataEntities.getRight());
                testDetail.setText(dataEntities.getExplain());
                break;
            }
        }
    }

    private void initSelectQuiz() {
        testSelectA.setText(dataEntities.getOption().get(0));
        testSelectB.setText(dataEntities.getOption().get(1));
        testSelectC.setText(dataEntities.getOption().get(2));
        testSelectD.setText(dataEntities.getOption().get(3));
    }

    private void initClickBtn() {
        examQuizJudgeA.setOnClickListener(this);
        examQuizJudgeB.setOnClickListener(this);

        examQuizSelectA.setOnClickListener(this);
        examQuizSelectB.setOnClickListener(this);
        examQuizSelectC.setOnClickListener(this);
        examQuizSelectD.setOnClickListener(this);
    }

    public void setQuizType(int position) {
        int type = Integer.parseInt(dataEntities.getTtype());
        switch (type) {
            case 1: {
                examQuizSelect.setVisibility(View.VISIBLE);
                examQuizJudge.setVisibility(View.GONE);
                break;
            }
            case 2: {
                examQuizSelect.setVisibility(View.GONE);
                examQuizJudge.setVisibility(View.VISIBLE);
                break;
            }
        }
    }

    private void initSelectResult(int result) {
        if (testActivity == null) {
            testActivity = (TestActivity) getActivity();
        }
        testActivity.addSelectAnswer(position, result);
//        if (isSecondTest) {
//            examQuizSelectAIcon.setImageResource(R.mipmap.exam_a);
//            examQuizSelectBIcon.setImageResource(R.mipmap.exam_b);
//            examQuizSelectCIcon.setImageResource(R.mipmap.exam_c);
//            examQuizSelectDIcon.setImageResource(R.mipmap.exam_d);
//        } else {
        examQuizSelectA.setClickable(false);
        examQuizSelectB.setClickable(false);
        examQuizSelectC.setClickable(false);
        examQuizSelectD.setClickable(false);
//        }

        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                testActivity.selectRightAnswer(position + 1);
            }
        };
        timer = new Timer();
        timer.schedule(timerTask, 1000);

        if (position < 19) {
            if (testActivity == null) {
                testActivity = (TestActivity) getActivity();
            }
        }

        if (result == selectAnswer) {

            if (testActivity == null) {
                testActivity = (TestActivity) getActivity();
            }
            testActivity.addQuizCorrect(position, true);

            switch (result) {
                case 1: {
                    examQuizSelectAIcon.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 2: {
                    examQuizSelectBIcon.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 3: {
                    examQuizSelectCIcon.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 4: {
                    examQuizSelectDIcon.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
            }
        } else {
//            testExplainLayout.setVisibility(View.VISIBLE);
            if (testActivity == null) {
                testActivity = (TestActivity) getActivity();
            }
            testActivity.addQuizCorrect(position, false);
//            switch (selectAnswer) {
//                case 1: {
//                    drawSelectResult(examQuizSelectAIcon);
//                    break;
//                }
//                case 2: {
//                    drawSelectResult(examQuizSelectBIcon);
//                    break;
//                }
//                case 3: {
//                    drawSelectResult(examQuizSelectCIcon);
//                    break;
//                }
//                case 4: {
//                    drawSelectResult(examQuizSelectDIcon);
//                    break;
//                }
//            }
            switch (result) {
                case 1: {
                    examQuizSelectAIcon.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 2: {
                    examQuizSelectBIcon.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 3: {
                    examQuizSelectCIcon.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 4: {
                    examQuizSelectDIcon.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
            }
        }
    }

    private void drawSelectResult(ImageView select) {
        examQuizSelectAIcon.setImageResource(R.mipmap.exam_answer_wrong);
        examQuizSelectBIcon.setImageResource(R.mipmap.exam_answer_wrong);
        examQuizSelectCIcon.setImageResource(R.mipmap.exam_answer_wrong);
        examQuizSelectDIcon.setImageResource(R.mipmap.exam_answer_wrong);

        select.setImageResource(R.mipmap.exam_answer_right);
    }

    private void initJudgeResult(int result) {
        if (testActivity == null) {
            testActivity = (TestActivity) getActivity();
        }
        testActivity.addSelectAnswer(position, result);
//        if (isSecondTest) {
//            examQuizJudgeAIcon.setImageResource(R.mipmap.exam_a);
//            examQuizJudgeBIcon.setImageResource(R.mipmap.exam_b);
//        } else {
        examQuizJudgeA.setClickable(false);
        examQuizJudgeB.setClickable(false);
//        }
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                testActivity.selectRightAnswer(position + 1);
            }
        };

        timer = new Timer();
        timer.schedule(timerTask, 1000);

        if (position < 19) {
            if (testActivity == null) {
                testActivity = (TestActivity) getActivity();
            }
        }

        if (result == judgeAnswer) {
            if (testActivity == null) {
                testActivity = (TestActivity) getActivity();
            }
            testActivity.addQuizCorrect(position, true);

            switch (result) {
                case 1: {
                    examQuizJudgeAIcon.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 2: {
                    examQuizJudgeBIcon.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
            }
        } else {
//            testExplainLayout.setVisibility(View.VISIBLE);
            if (testActivity == null) {
                testActivity = (TestActivity) getActivity();
            }
            testActivity.addQuizCorrect(position, false);
//            switch (judgeAnswer) {
//                case 1: {
//                    drawJudgeRuselt(examQuizJudgeAIcon);
//                    break;
//                }
//                case 2: {
//                    drawJudgeRuselt(examQuizJudgeBIcon);
//                    break;
//                }
//            }
            switch (result) {
                case 1: {
                    examQuizJudgeAIcon.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
                case 2: {
                    examQuizJudgeBIcon.setImageResource(R.mipmap.exam_answer_right);
                    break;
                }
            }
        }
    }

    private void drawJudgeRuselt(ImageView judge) {
        examQuizJudgeAIcon.setImageResource(R.mipmap.exam_answer_wrong);
        examQuizJudgeBIcon.setImageResource(R.mipmap.exam_answer_wrong);

        judge.setImageResource(R.mipmap.exam_answer_right);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.exam_quiz_judge_a: {
                initJudgeResult(1);
                checkIsAllAnswer();
                break;
            }
            case R.id.exam_quiz_judge_b: {
                initJudgeResult(2);
                checkIsAllAnswer();
                break;
            }
            case R.id.exam_quiz_select_a: {
                initSelectResult(1);
                checkIsAllAnswer();
                break;
            }
            case R.id.exam_quiz_select_b: {
                initSelectResult(2);
                checkIsAllAnswer();
                break;
            }
            case R.id.exam_quiz_select_c: {
                initSelectResult(3);
                checkIsAllAnswer();
                break;
            }
            case R.id.exam_quiz_select_d: {
                initSelectResult(4);
                checkIsAllAnswer();
                break;
            }
        }
    }

    private void checkIsAllAnswer() {
        if (testActivity.isAllAnswer()) {
            testActivity.initDialog(true);
        }
    }

    @Override
    public void onPause() {
        super.onPause();

    }

    @Override
    public void onStop() {
        super.onStop();
        timer.cancel();
        timer.purge();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }


}
